//
//  EvManageViewController.m
//  Manage
//
//  Created by 杨康 on 2017/11/11.
//  Copyright © 2017年 杨康. All rights reserved.
//

#import "EvManageViewController.h"
#import "EvDetailsViewController.h"
#import "DeviceModel.h"
@interface EvManageViewController ()
@property(nonatomic,assign)NSInteger acType;
@property(nonatomic,strong)ykTableView *tableView;
@end

@implementation EvManageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"电动车管理";
    
    UIButton *leftButton=(UIButton *)self.navigationItem.leftBarButtonItem.customView;
    leftButton.selected=YES;
    
    
    self.tableView =[SingLeton myTableViewWithFrame:CGRectMake(0, TOP_HEIGHT+50, SCREEN_WIDTH, SCREEN_HEIGHT-TOP_HEIGHT-50)  withRefresh:NO didSelect:^(ykTableView *tableView, NSIndexPath *indexPath) {
        EvmCell *cell=[tableView cellForRowAtIndexPath:indexPath];
        EvDetailsViewController *vc=[[EvDetailsViewController alloc]init];
        vc.deviceid=cell.dataDic.model.deviceid;
        switch (self.acType) {
            case 0:
            vc.type=@"未审核";
                break;
            case 1:
                vc.type=@"已审核";
                break;
            case 2:
                vc.type=@"未通过";
                break;
                
            default:
                break;
        }
        
        [self.navigationController pushViewController:vc animated:YES];
        
    }];
    
    [self.view addSubview:self.tableView];
    
    
    
    
    
    UISegmentedControl *TypeSegmentedControl = [[UISegmentedControl alloc] initWithItems:
                                                [NSArray arrayWithObjects:
                                                 @"未审核",
                                                 @"已审核",
                                                 @"未通过",
                                                 nil]];
    TypeSegmentedControl.apportionsSegmentWidthsByContent = YES;
    TypeSegmentedControl.layer.masksToBounds=YES;
    TypeSegmentedControl.layer.cornerRadius=5;
    TypeSegmentedControl.tintColor = [UIColor colorF];
    NSDictionary *dics = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor color6],NSForegroundColorAttributeName,[UIFont systemFontOfSize:12.0],NSFontAttributeName,nil];
    [TypeSegmentedControl setTitleTextAttributes:dics forState:UIControlStateNormal];
    [TypeSegmentedControl setBackgroundImage:[NetworkPath createImageWithColor:[UIColor colorQ]]
                                    forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
    [TypeSegmentedControl setBackgroundImage:[NetworkPath createImageWithColor:[UIColor whiteColor]]
                                    forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    
    [TypeSegmentedControl addTarget:self action:@selector(TypeAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:TypeSegmentedControl];
    [TypeSegmentedControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.top.mas_equalTo(TOP_HEIGHT+10);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH-20, 30));
        
    }];
    
    
    TypeSegmentedControl.selectedSegmentIndex=0;
    [self TypeAction:TypeSegmentedControl];
}

- (void)TypeAction:(UISegmentedControl *)segmentedControl
{
    self.acType = segmentedControl.selectedSegmentIndex;
    switch (self.acType) {
        case 0:
        {
            
            NSString *URLString = @"/axq/admin/device/list";
            NSDictionary *parameters = @{@"status": @""};
            [NetworkPath requestWithMethod:0 andURLString:URLString andParameters:parameters RequestSuccess:^(NSURLResponse *response, id responseObject) {
                NSLog(@"%@",responseObject);
                self.tableView.className=@"EvmCell";
                NSMutableArray *itemArr=[NSMutableArray array];
                //1.先判断responseObject是不是字典
                if ([responseObject isKindOfClass:[NSDictionary class]]) {
                    //2.再判断字典有没有code key
                    if (responseObject[@"code"]) {
                        //3. code转化为字符串 防止类型崩溃
                       NSString *code = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
                        if([code isEqualToString:@"10008"]){
                            //4.再判断有没有data key
                            if (responseObject[@"data"]) {
                                //5.再判断data 是不是字典（数组）
                                if ([responseObject[@"data"] isKindOfClass:[NSArray class]]) {
                                   NSArray *data=responseObject[@"data"];
                                    //6.判断数组有没有数据
                                    if (data.count) {
                                        for (int i=0; i<data.count; i++) {
                                            //7.判断是不是字典
                                            if ([data[i] isKindOfClass:[NSDictionary class]]) {
                                                NSDictionary *dic=data[i];
                                                DeviceModel *model=[DeviceModel modelWithDict:dic];
                                
                                                EvmData *dataObj=[[EvmData alloc]init];
                                                if (![NSString isNULLString:model.img1]) {
                                                    dataObj.image=model.img1;
                                                }else
                                                {
                                                    if (![NSString isNULLString:model.img2]) {
                                                        dataObj.image=model.img2;
                                                    }else
                                                    {
                                                        if (![NSString isNULLString:model.img3]) {
                                                            dataObj.image=model.img3;
                                                        }else
                                                        {
                                                          dataObj.image=@"";
                                                        }
                                                    }
                                                }
                                                dataObj.name=model.devicename;
                                                dataObj.storeName=model.shopname;
                                                dataObj.brand=model.brandname;
                                                dataObj.color=model.color;
                                                dataObj.state=model.status;
                                                dataObj.model=model;
                                                [itemArr addObject:dataObj];
                                                
                                                
                                                
                                                
                                                
                                            }
                                        }
                                    }
                                }
                                
                            }
                            
                        }
                        else
                        {
                            NSString *str=responseObject[@"msg"];
                            
                            [self.view makeToast:str];
                        }
                        
                        
                        
                    }
                }
                
                
                [self.tableView.dataArray addObject:itemArr];
                [self.tableView reloadData];
                
                
                
                
            } RequestFailed:^(NSError *error) {
                self.tableView.className=@"EvmCell";
                NSMutableArray *itemArr=[NSMutableArray array];
                [self.tableView.dataArray addObject:itemArr];
                [self.tableView reloadData];
            }];
            
        }
            break;
        case 1:
        {
            NSString *URLString = @"/axq/admin/device/list";
            NSDictionary *parameters = @{@"status": @"success"};
            [NetworkPath requestWithMethod:0 andURLString:URLString andParameters:parameters RequestSuccess:^(NSURLResponse *response, id responseObject) {
                NSLog(@"%@",responseObject);
                self.tableView.className=@"EvmCell";
                NSMutableArray *itemArr=[NSMutableArray array];
                NSString *code=[NetworkPath getValue:@"code" from:responseObject];
                //上面方法都会有字符串 所以用length 判断
                if (code.length) {
                   if([code isEqualToString:@"10008"])
                   {
                       if ([NetworkPath isNSArray:@"data" from:responseObject]) {
                          NSArray *data=responseObject[@"data"];
                           for (int i=0; i<data.count; i++) {
                               if ([data[i] isKindOfClass:[NSDictionary class]]) {
                                   DeviceModel *model=[DeviceModel modelWithDict:data[i]];
                                   EvmData *dataObj=[[EvmData alloc]init];
                                   if (![NSString isNULLString:model.img1]) {
                                       dataObj.image=model.img1;
                                   }else
                                   {
                                       if (![NSString isNULLString:model.img2]) {
                                           dataObj.image=model.img2;
                                       }else
                                       {
                                           if (![NSString isNULLString:model.img3]) {
                                               dataObj.image=model.img3;
                                           }else
                                           {
                                               dataObj.image=@"";
                                           }
                                       }
                                   }
                                   dataObj.name=model.devicename;
                                   dataObj.storeName=model.shopname;
                                   dataObj.brand=model.brandname;
                                   dataObj.color=model.color;
                                   dataObj.state=model.status;
                                   dataObj.model=model;
                                   [itemArr addObject:dataObj];
                               }
                           }
                       }
                   }else
                   {
                       
                       NSString *str=[NetworkPath getValue:@"msg" from:responseObject];
                       
                       [self.view makeToast:str];
                   }
                }
                
                [self.tableView.dataArray addObject:itemArr];
                [self.tableView reloadData];
            } RequestFailed:^(NSError *error) {
                self.tableView.className=@"EvmCell";
                NSMutableArray *itemArr=[NSMutableArray array];
                [self.tableView.dataArray addObject:itemArr];
                [self.tableView reloadData];
            }];
            
        }
            break;
        case 2:
        {
            NSString *URLString = @"/axq/admin/device/list";
            NSDictionary *parameters = @{@"status": @"failed"};
            [NetworkPath requestWithMethod:0 andURLString:URLString andParameters:parameters RequestSuccess:^(NSURLResponse *response, id responseObject) {
                NSLog(@"%@",responseObject);
                self.tableView.className=@"EvmCell";
                NSMutableArray *itemArr=[NSMutableArray array];
                NSString *code=[NetworkPath getValue:@"code" from:responseObject];
                //上面方法都会有字符串 所以用length 判断
                if (code.length) {
                    if([code isEqualToString:@"10008"])
                    {
                        if ([NetworkPath isNSArray:@"data" from:responseObject]) {
                            NSArray *data=responseObject[@"data"];
                            for (int i=0; i<data.count; i++) {
                                if ([data[i] isKindOfClass:[NSDictionary class]]) {
                                    DeviceModel *model=[DeviceModel modelWithDict:data[i]];
                                    EvmData *dataObj=[[EvmData alloc]init];
                                    if (![NSString isNULLString:model.img1]) {
                                        dataObj.image=model.img1;
                                    }else
                                    {
                                        if (![NSString isNULLString:model.img2]) {
                                            dataObj.image=model.img2;
                                        }else
                                        {
                                            if (![NSString isNULLString:model.img3]) {
                                                dataObj.image=model.img3;
                                            }else
                                            {
                                                dataObj.image=@"";
                                            }
                                        }
                                    }
                                    dataObj.name=model.devicename;
                                    dataObj.storeName=model.shopname;
                                    dataObj.brand=model.brandname;
                                    dataObj.color=model.color;
                                    dataObj.state=model.status;
                                    dataObj.model=model;
                                    [itemArr addObject:dataObj];
                                }
                            }
                        }
                    }else
                    {
                        
                        NSString *str=[NetworkPath getValue:@"msg" from:responseObject];
                        
                        [self.view makeToast:str];
                    }
                }
                
                [self.tableView.dataArray addObject:itemArr];
                [self.tableView reloadData];
            } RequestFailed:^(NSError *error) {
                self.tableView.className=@"EvmCell";
                NSMutableArray *itemArr=[NSMutableArray array];
                [self.tableView.dataArray addObject:itemArr];
                [self.tableView reloadData];
            }];
        }
            break;
        
            
        default:
            break;
    }
}

@end
